package com.treasure.mall.zbqmallapi.controller.tools;

import com.treasure.mall.biz.dto.user.ModifyBalanceDTO;
import com.treasure.mall.biz.entity.user.UserPO;
import com.treasure.mall.biz.remote.wx.WxTemplateMessageService;
import com.treasure.mall.biz.remote.wx.ZbWxMpService;
import com.treasure.mall.biz.service.core.tools.ToolsService;
import com.treasure.mall.biz.service.core.user.UserService;
import com.treasure.mall.biz.vo.user.UserVO;
import com.treasure.mall.zbq.common.annotation.CurrentUser;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.base.ResultCodeEnum;
import com.treasure.mall.zbq.common.exception.ApiException;
import com.treasure.mall.zbq.common.ienum.log.AdvanceLogType;
import com.treasure.mall.zbqmallapi.annotation.AccessControl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 *
 * <p> 工具，该工具接口，只为方便，不对外操作
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.zbqmallapi.controller.tools
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/8/22 10:56 上午
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 * 
 */
@RestController
@RequestMapping("/tools")
@AccessControl(needLogin = true)
@CrossOrigin
public class ToolsController {

    private final static String PWD = "zbq@1234";
    private final static String ACCOUNT = "17606540115";


    @Autowired
    private UserService userService;
    @Autowired
    private ToolsService toolsService;
    @Autowired
    private WxTemplateMessageService wxTemplateMessageService;
    @Autowired
    private ZbWxMpService zbWxMpService;

    /**
     * 登录
     *
     * @param mobile
     * @param password
     * @return
     */
    @PostMapping("/login")
    @AccessControl
    public ApiResult login(String mobile, String password) {
        UserPO user = userService.findByUsername(mobile);
        if (user == null) {
            return ApiResult.error("账号或密码错误");
        }
        if (!ACCOUNT.equals(mobile) || !PWD.equals(password)) {
            return ApiResult.error("账号或密码错误");
        }
        UserVO vo = userService.who(user.getUserId(), 0L);
        if (vo == null) {
            return ApiResult.error("账号或密码错误");
        }
        return ApiResult.ok(vo.getLoginToken());
    }

    /**
     * 修改用聊天等级
     *
     * @param mobile 手机号码
     * @param level  等级 0~9
     * @return
     */
    @PostMapping("/modify/chat/level")
    public ApiResult modifyUserChatLevel(@CurrentUser UserPO userPO, @RequestParam String mobile, int level) {
        checkUser(userPO);
        UserPO user = userService.findByUsername(mobile);
        if (user == null) {
            return ApiResult.error("手机号码不存在");
        }

        toolsService.modifyUserChatLevel(mobile, user.getUserId(), level);

        return ApiResult.ok();
    }

    /**
     * 修改店铺名称
     *
     * @param mobile   手机号码
     * @param shopName 店铺名称
     * @return
     */
    @PostMapping("/modify/shop/name")
    public ApiResult modifyShopName(@CurrentUser UserPO userPO, @RequestParam String mobile, @RequestParam String shopName) {
        checkUser(userPO);
        UserPO user = userService.findByUsername(mobile);
        if (user == null || user.getMerchantId() == null) {
            return ApiResult.error("手机号码不存在");
        }

        toolsService.modifyShopName(mobile, user.getMerchantId(), shopName);

        return ApiResult.ok();
    }

    /**
     * 开启用户288权限
     *
     * @param mobile
     * @return
     */
    @PostMapping("/modify/merchant/permissions")
    public ApiResult modifyMerchantPermissions(@CurrentUser UserPO userPO, @RequestParam String mobile) {
        checkUser(userPO);
        UserPO user = userService.findByUsername(mobile);
        if (user == null) {
            return ApiResult.error("手机号码不存在");
        }

        toolsService.modifyMerchantPermissions(mobile, user.getUserId());

        return ApiResult.ok();
    }


    /**
     * 增加店铺关注数
     *
     * @param mobile 手机号
     * @param count  增加数量
     * @return
     */
    @PostMapping("/modify/merchant/follow")
    public ApiResult modifyMerchantFollowCount(@CurrentUser UserPO userPO, @RequestParam String mobile, int count) {
        checkUser(userPO);
        UserPO user = userService.findByUsername(mobile);
        if (user == null || user.getMerchantId() == null) {
            return ApiResult.error("手机号码不存在");
        }

        toolsService.modifyMerchantFollowCount(mobile, user.getMerchantId(), count);

        return ApiResult.ok();
    }

    /**
     * 修改商家保证金
     *
     * @param mobile 手机号
     * @param amount 保证金额度
     * @return
     */
    @PostMapping("/modify/merchant/earnest/money")
    public ApiResult modifyMerchantEarnestMoney(@CurrentUser UserPO userPO, @RequestParam String mobile, @RequestParam BigDecimal amount) {
        checkUser(userPO);
        UserPO user = userService.findByUsername(mobile);
        if (user == null || user.getMerchantId() == null) {
            return ApiResult.error("手机号码不存在");
        }
        toolsService.modifyMerchantEarnestMoney(mobile, user.getMerchantId(), amount);
        return ApiResult.ok();
    }


    /**
     * 修改用户为达人
     *
     * @param mobile 手机号
     * @return
     */
    @PostMapping("/modify/user/level")
    public ApiResult modifyUserLevel(@CurrentUser UserPO userPO, @RequestParam String mobile) {
        checkUser(userPO);
        UserPO user = userService.findByUsername(mobile);
        if (user == null) {
            return ApiResult.error("手机号码不存在");
        }
        toolsService.modifyUserLevel(mobile, user.getUserId());
        return ApiResult.ok();
    }


    /**
     * 修改用户余额
     *
     * @param userPO
     * @param mobile
     * @param amount
     * @param remark
     * @return
     */
    @PostMapping("/modify/user/balance")
    public ApiResult modifyUserBalance(@CurrentUser UserPO userPO, @RequestParam String mobile, @RequestParam BigDecimal amount, String remark) {
        checkUser(userPO);
        UserPO user = userService.findByUsername(mobile);
        if (user == null) {
            return ApiResult.error("手机号码不存在");
        }
        ModifyBalanceDTO modifyBalanceDTO = new ModifyBalanceDTO();
        modifyBalanceDTO.setLogType(AdvanceLogType.MANUAL);
        modifyBalanceDTO.setOpId(userPO.getUserId().intValue());
        modifyBalanceDTO.setOpName(userPO.getUsername());
        modifyBalanceDTO.setRemark(remark);
        modifyBalanceDTO.setAmount(amount);
        modifyBalanceDTO.setId(user.getUserId());
        if (StringUtils.isEmpty(modifyBalanceDTO.getRemark())) {
            modifyBalanceDTO.setRemark("系统处理");
        }
        userService.modifyBalance(modifyBalanceDTO);

        toolsService.insertLog("修改用户余额", mobile, "修改用户[" + mobile + "]余额" + amount + ",说明:" + remark);
        return ApiResult.ok();
    }


    private void checkUser(UserPO currentUser) {
        if (currentUser == null || !ACCOUNT.equals(currentUser.getMobile())) {
            throw new ApiException(ApiResult.error(ResultCodeEnum.NOT_LOGIN));
        }
    }

    /**
     * 发送开播模板通知
     *
     * @param roomId
     * @return
     */
    @PostMapping("/sendTemplateMessage")
    public ApiResult sendTemplateMessage(@CurrentUser UserPO userPO, @RequestParam Long roomId) {
        checkUser(userPO);
//        wxTemplateMessageService.sendLiveRoomMessage(roomId);
//        toolsService.insertLog("发送开播模板通知", "", "发送直播间模板通知，直播间ID：[" + roomId + "]" + "");
        return ApiResult.ok();
    }

    /**
     * 初始化关注用户
     *
     * @param userPO
     * @return
     */
    @PostMapping("/initSubscribeUser")
    public ApiResult initSubscribeUser(@CurrentUser UserPO userPO) {
        checkUser(userPO);
        zbWxMpService.initSubScribeUsers();
        return ApiResult.ok();
    }


}
